import { useEffect } from 'react';

export const useGameTimer = (gameStarted, setGameInfo, setPetState) => {
  useEffect(() => {
    if (!gameStarted) return;
    
    const gameTimer = setInterval(() => {
      setGameInfo(prev => ({
        ...prev,
        day: prev.day + 1
      }));
      
      setPetState(prev => {
        const newHunger = Math.max(prev.hunger - 5, 0);
        const newHappiness = Math.max(prev.happiness - 3, 0);
        const newCleanliness = Math.max(prev.cleanliness - 4, 0);
        const newEnergy = prev.isSleeping ? Math.min(prev.energy + 10, 100) : Math.max(prev.energy - 2, 0);
        
        let newHealth = prev.health;
        let newIsIll = prev.isIll;
        
        if (newHunger < 20 || newHappiness < 20 || newCleanliness < 30) {
          newHealth = Math.max(newHealth - 5, 0);
        }
        
        if (newHealth < 30 && Math.random() < 0.3) {
          newIsIll = true;
        }
        
        if (newIsIll) {
          newHealth = Math.max(newHealth - 3, 0);
        }
        
        const newAge = prev.age + 1;
        let newStage = prev.stage;
        
        if (newAge >= 100) newStage = 'elder';
        else if (newAge >= 50) newStage = 'adult';
        else if (newAge >= 20) newStage = 'young';
        
        return {
          ...prev,
          hunger: newHunger,
          happiness: newHappiness,
          health: newHealth,
          energy: newEnergy,
          cleanliness: newCleanliness,
          age: newAge,
          stage: newStage,
          isIll: newIsIll
        };
      });
    }, 10000);
    
    return () => clearInterval(gameTimer);
  }, [gameStarted, setGameInfo, setPetState]);
};